package collection.queue.block_queue;

import org.junit.Test;

import java.util.concurrent.SynchronousQueue;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/16 7:41 PM
 */
public class SynchronousQueueTest {

    /**
     * @see SynchronousQueue
     */
    @Test
    public void test1() {
        /**
         * 默认非公平锁
         */
        SynchronousQueue queue = new SynchronousQueue();

        SynchronousQueue queue2 = new SynchronousQueue(true);
    }

    @Test
    public void test2() {
        SynchronousQueue queue = new SynchronousQueue();
        queue.offer("xxx");
        System.out.println(queue.size());

        queue.add("xxx");
    }

    @Test
    public void test3() throws InterruptedException {

        SynchronousQueue queue = new SynchronousQueue();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    queue.put("" + i);

                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(() -> {
            while (true) {

                try {
                    Object o = queue.take();
                    System.out.println(o);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        thread2.start();
        thread2.join();

    }
}
